﻿Imports Cls_Common
Imports VSModel
Imports System.Data

Partial Class FrontEnd_Control_RaoVat_TinLienQuan
    Inherits System.Web.UI.UserControl

#Region "Sub and Function"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If (Not Page.RouteData.Values("Id") Is Nothing) Then
                hidID.Value = Page.RouteData.Values("Id")
                LoadTinhThanh()
                BindToGrid(1, ddlTinhThanh.SelectedValue)
                BindToGrid1(1, ddlTinhThanh.SelectedValue)
            End If
        End If
    End Sub

    Protected Sub LoadTinhThanh()
        Using Data As New VSEntities
            Dim p = (From q In Data.KhuVucs Select q).ToList
            For Each a In p
                'insert khu vuc
                Dim itm As New ListItem
                itm.Text = a.Name
                itm.Value = 0
                itm.Attributes.Add("disabled", "disabled")
                itm.Attributes.Add("class", "Khuvuc")
                ddlTinhThanh.Items.Add(itm)

                Dim _khuvucId = a.Id
                Dim k = (From q In Data.DonViHanhChinhs Where q.KhuVucId = _khuvucId Order By q.Title Ascending Select q).ToList
                For Each item In k
                    Dim itm1 As New ListItem
                    itm1.Value = item.Id
                    itm1.Text = "---" & item.Title
                    itm1.Enabled = True
                    ddlTinhThanh.Items.Add(itm1)
                Next
            Next
        End Using
    End Sub

    Private Sub BindToGrid(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities
            Dim count As Integer
            Dim intID As Integer = hidID.Value
            Dim h = (From k In Data.RaoVats Where k.RaovatId = intID Select k).FirstOrDefault
            Dim p As List(Of RaoVat)
            p = (From q In Data.RaoVats Where ((q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0)) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) And (q.CateID = h.CateID) _
                                                And (q.RaovatId <> intID) _
                                                Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 10).Take(10).ToList

            count = (From q In Data.RaoVats Where (q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                            And (q.RaovatId <> intID) _
                                            Or q.eEffectiveDate Is Nothing) And (q.CateID = h.CateID) Select q.RaovatId).Count

            'cung model
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 10, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptShow
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub
    Private Sub BindToGrid1(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities
            'cung gia
            Dim countCungGia As Integer
            Dim intID As Integer = hidID.Value
            Dim h = (From t In Data.RaoVats Where t.RaovatId = intID Select t).FirstOrDefault
            Dim intGia As Integer = 0
            If Not h Is Nothing Then
                intGia = h.OriginalPrice
            End If

            Dim k As List(Of RaoVat)
            k = (From q In Data.RaoVats Where ((q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0)) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                                And (q.OriginalPrice >= (intGia - intGia * 0.1) And q.OriginalPrice <= (intGia + intGia * 0.1)) _
                                                And (q.RaovatId <> intID) _
                                                Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 10).Take(10).ToList

            countCungGia = (From q In Data.RaoVats Where (q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) _
                                           And (q.OriginalPrice >= (intGia - intGia * 0.1) And q.OriginalPrice <= (intGia + intGia * 0.1)) _
                                            And (q.RaovatId <> intID) _
                                             Select q.RaovatId).Count

            'cung model
            If countCungGia > 0 Then
                hidCount1.Value = countCungGia
                Create_Pager1(hidCount1.Value, ipage, 10, 10)
            Else
                hidCount1.Value = 0
                With rptPage1
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptCungGiaBan
                .DataSource = k
                .DataBind()
            End With
        End Using
    End Sub
#End Region

#Region "Event for control"

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub

    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub
    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lbtTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle.ToolTip
        BindToGrid(1, lbtTitle.ToolTip)

    End Sub

    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptShow.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"

        Dim lblTinhTrang As Label = CType(e.Item.FindControl("lblTinhTrang"), Label)

        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("80-80", row.ImagePreview.ToString)

        Dim ltrThongSo As Literal = CType(e.Item.FindControl("ltrThongSo"), Literal)

        Dim ltrMota As Literal = CType(e.Item.FindControl("ltrMota"), Literal)
        Dim ltrKhuVuc As Literal = CType(e.Item.FindControl("ltrKhuVuc"), Literal)
        Using Data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = Data.uspThongsocobanSelect(row.RaovatId).FirstOrDefault
            If Not p Is Nothing Then
                ltrThongSo.Text = p.XuatXu & "," & p.MauNgoaiThat & "," & p.HeThongNhienLieu & "," & p.HopSo
                lblTinhTrang.Text = "[" & p.TinhTrang & "]"
                ltrMota.Text = p.Description
            End If
            Dim h = (From l In Data.DonViHanhChinhs Where l.Id = row.TinhTP Select l).FirstOrDefault
            If Not h Is Nothing Then
                ltrKhuVuc.Text = h.Title
            End If

        End Using
        Dim ltrGiaBan As Literal = CType(e.Item.FindControl("ltrGiaBan"), Literal)
        ltrGiaBan.Text = FormatCurrency(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"

        Dim ltrGiaTuongDuong As Literal = CType(e.Item.FindControl("ltrGiaTuongDuong"), Literal)
        ltrGiaTuongDuong.Text = FormatCurrency(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"

        Dim lblHoTen As Label = CType(e.Item.FindControl("lblHoTen"), Label)
        Dim ltrDienThoai As Literal = CType(e.Item.FindControl("ltrDienThoai"), Literal)
        Dim ltrDiaChi As Literal = CType(e.Item.FindControl("ltrDiaChi"), Literal)

        lblHoTen.Text = IIf(IsNothing(row.HoTen) = True, "", row.HoTen) & Environment.NewLine
        ltrDiaChi.Text = IIf(IsNothing(row.DiaChi) = True, "", row.DiaChi) & " " & ltrKhuVuc.Text & vbNewLine
        ltrDienThoai.Text = IIf(IsNothing(row.Phone) = True, "", row.Phone)
        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrDienThoai.Text = ltrDienThoai.Text & " - " & row.PhoneOther
        End If

    End Sub

    Protected Sub ddlTinhThanh_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTinhThanh.SelectedIndexChanged
        BindToGrid(1, ddlTinhThanh.SelectedValue)
        BindToGrid1(1, ddlTinhThanh.SelectedValue)
    End Sub

#End Region
#Region "Tin cung gia"

    Sub Create_Pager1(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page1.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page1.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage1
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage1.ItemDataBound
        Dim lnkTitle1 As LinkButton
        lnkTitle1 = e.Item.FindControl("lnkTitle1")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle1)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle1.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle1.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle1.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile1 As LinkButton = CType(sender, LinkButton)
        BindToGrid1(lnkTile1.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast1.Enabled = True
        lnkFirst1.Enabled = True
        If CInt(lnkTile1.ToolTip) = hidIndex_page.Value Then
            lnkLast1.Enabled = False
        ElseIf CInt(lnkTile1.ToolTip) = 1 Then
            lnkFirst1.Enabled = False
        End If
    End Sub

    Protected Sub lnkFirst1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst1.Click
        If hidCur_Page1.Value > 1 Then
            hidCur_Page1.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page1.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lnkLast1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast1.Click
        hidCur_Page1.Value = hidCur_Page1.Value + 1
        BindToGrid1(hidCur_Page1.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lbtTitle1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle1 As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle1.ToolTip
        BindToGrid1(1, lbtTitle1.ToolTip)
    End Sub

    Protected Sub rptCungGiaBan_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCungGiaBan.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"
        Dim lblTinhTrang As Label = CType(e.Item.FindControl("lblTinhTrang"), Label)

        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("80-80", row.ImagePreview.ToString)

        Dim ltrThongSo As Literal = CType(e.Item.FindControl("ltrThongSo"), Literal)

        Dim ltrMota As Literal = CType(e.Item.FindControl("ltrMota"), Literal)
        Dim ltrKhuVuc As Literal = CType(e.Item.FindControl("ltrKhuVuc"), Literal)
        Using Data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = Data.uspThongsocobanSelect(row.RaovatId).FirstOrDefault
            If Not p Is Nothing Then
                ltrThongSo.Text = p.XuatXu & "," & p.MauNgoaiThat & "," & p.HeThongNhienLieu & "," & p.HopSo
                ltrMota.Text = p.Description
                lblTinhTrang.Text = "[" & p.TinhTrang & "]"
            End If
            Dim h = (From l In Data.DonViHanhChinhs Where l.Id = row.TinhTP Select l).FirstOrDefault
            If Not h Is Nothing Then
                ltrKhuVuc.Text = h.Title
            End If

        End Using
        Dim ltrGiaBan As Literal = CType(e.Item.FindControl("ltrGiaBan"), Literal)
        ltrGiaBan.Text = FormatCurrency(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"

        Dim ltrGiaTuongDuong As Literal = CType(e.Item.FindControl("ltrGiaTuongDuong"), Literal)
        ltrGiaTuongDuong.Text = FormatCurrency(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"

        Dim lblHoTen As Label = CType(e.Item.FindControl("lblHoTen"), Label)
        Dim ltrDienThoai As Literal = CType(e.Item.FindControl("ltrDienThoai"), Literal)
        Dim ltrDiaChi As Literal = CType(e.Item.FindControl("ltrDiaChi"), Literal)

        lblHoTen.Text = IIf(IsNothing(row.HoTen) = True, "", row.HoTen) & Environment.NewLine
        ltrDiaChi.Text = IIf(IsNothing(row.DiaChi) = True, "", row.DiaChi) & " " & ltrKhuVuc.Text & vbNewLine
        ltrDienThoai.Text = IIf(IsNothing(row.Phone) = True, "", row.Phone)
        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrDienThoai.Text = ltrDienThoai.Text & " - " & row.PhoneOther
        End If

    End Sub

#End Region
End Class
